<?php

namespace App\Http\Middleware;

use App\Rep\NumberExtractorRep;
use Closure;
use Illuminate\Http\Request;

class MeployeeCheckLogin
{
    /**
     * 员工抽号机输入卡号查询中间件
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->header("token")??$request->get('token');
        //p($token);
        if(empty($token)){
            return re(1020);
        }else{
            $user_info = NumberExtractorRep::getUserInfoForToken($token);
            if(empty($user_info)){
                return re(5003);
            }else{
                NumberExtractorRep::resetUserTokenExpire($token);
                $request->offsetSet("user_name",$user_info['user_name']);
                $request->offsetSet("user_id",$user_info["user_id"]);
                $request->offsetSet("card_number",$user_info["card_number"]);
                $request->offsetSet("job_type",$user_info["job_type"]);
                $request->offsetSet("parent_username",$user_info["parent_username"]);
            }
        }

        return $next($request);
    }
}
